<?php
namespace Main\Controller;
use Think\Controller;
class CurrController extends AppController {

    function __construct()
    {
        parent::__construct();
        $this->M_curr = M('Curr');
        $this->M_curr_cate = M('Curr_cate');
    }

    public function index(){
        $curr_cate = $this->M_curr_cate->select();
        $this->assign('curr_cate',$curr_cate);
        $whe = '1=1';
        if(I('get.curr_name')){
            $this->assign('curr_name',I('get.curr_name'));
            $whe .= ' AND c.curr_name LIKE\'%'.I('get.curr_name').'%\'';
        }
        if(I('get.curr_cate_id')){
            $this->assign('curr_cate_id',I('get.curr_cate_id'));
            $whe .= ' AND c.curr_cate_id = '.I('get.curr_cate_id');
        }
        $where = $this->get_where($whe);
        $count = $this->M_curr->where($where)->join('as c left join sp_curr_cate as c_c ON c.curr_cate_id = c_c.curr_cate_id')->count();
        $page = $this->page($count,15);
        $list = $this->M_curr->where($where)->field('c.*,c_c.curr_cate_name')->join('as c left join sp_curr_cate as c_c ON c.curr_cate_id = c_c.curr_cate_id')->limit($page['f'].",".$page['l'])->order('c.sort asc')->select();
        $this->assign('list',$list);
        $this->display();
    }

    public function edit(){
        $id = I('get.id',0,'int');
        $curr_cate = $this->M_curr_cate->select();
        $this->assign('curr_cate',$curr_cate);
        if(!empty($id)){
            $info = $this->M_curr->find($id);
            $this->assign('info',$info);
        }
        if(IS_POST){
            $this->M_curr->curr_name = I('post.curr_name');
            $this->M_curr->introduce = I('post.introduce');
            $this->M_curr->curr_cate_id = I('post.curr_cate_id');
            $this->M_curr->price = I('post.price');
            $this->M_curr->sort = I('post.sort',0,'intval');
            $this->M_curr->add_time = time();
            //图片处理
            $upload = new \Think\Upload();
            $upload->maxSize   =     3145728 ;// 设置附件上传大小，默认3M
            $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath  =     './'; // 设置附件上传根目录
            $upload->savePath  =     './Uploads/curr_img/'.$this->uid.'/'; // 设置附件上传（子）目录// 上传文件
            $img_info   =   $upload->upload();
            if(!$img_info) {// 上传错误提示错误信息
                //$this->error($upload->getError());
            }else{// 上传成功
                $curr_img = $img_info['curr_img']['savepath'].$img_info['curr_img']['savename'];
                $this->M_curr->curr_img = $curr_img;
            }
            if($id){
                if($this->M_curr->save())
                die($this->success('修改成功',U('curr/index')));
            }else{
                $this->M_curr->uid = $this->uid;
                if($this->M_curr->add())
                die($this->success('添加成功',U('curr/index')));
            }
        }
        $this->display();
    }


    public function is_show(){
        if(!IS_AJAX)
            die($this->ajaxReturn('对不起，您不是ajax访问'));
        $id = I('get.id',0,'intval');
        if(!$id)
            die($this->ajaxReturn('参数错误！'));
        $info = $this->M_curr->find($id);
        $is_show = $info['is_show']?0:1;
        $this->M_curr->is_show = $is_show;
        $this->M_curr->save();
        $is_show?$this->ajaxReturn(array('result'=>'yes')):$this->ajaxReturn(array('result'=>'no'));
    }

    public function is_home(){
        if(!IS_AJAX)
            die($this->ajaxReturn('对不起，您不是ajax访问'));
        $id = I('get.id',0,'intval');
        if(!$id)
            die($this->ajaxReturn('参数错误！'));
        $info = $this->M_curr->find($id);
        $is_home = $info['is_home']?0:1;
        $this->M_curr->is_home = $is_home;
        $this->M_curr->save();
        $is_home?$this->ajaxReturn(array('result'=>'yes')):$this->ajaxReturn(array('result'=>'no'));
    }

    public function is_hot(){
        if(!IS_AJAX)
            die($this->ajaxReturn('对不起，您不是ajax访问'));
        $id = I('get.id',0,'intval');
        if(!$id)
            die($this->ajaxReturn('参数错误！'));
        $info = $this->M_curr->find($id);
        $is_hot = $info['is_hot']?0:1;
        $this->M_curr->is_hot = $is_hot;
        $this->M_curr->save();
        $is_hot?$this->ajaxReturn(array('result'=>'yes')):$this->ajaxReturn(array('result'=>'no'));
    }





}